# -*- coding: utf-8 -*-
"""
重新保存模型，去掉对自定义模块的依赖
"""

import joblib
import os

BASE_DIR = os.path.dirname(__file__)
MODELS_DIR = os.path.join(BASE_DIR, "models")

# 输入/输出文件名
SRC_MODELS = {
    "model.pkl": "model_clean.pkl",  # 作物推荐
    "improved_crop_model.pkl": "improved_crop_model_clean.pkl"  # 产量预测
}

def resave_model(src_name, dst_name):
    src_path = os.path.join(MODELS_DIR, src_name)
    dst_path = os.path.join(MODELS_DIR, dst_name)

    print(f"加载 {src_path} ...")
    model = joblib.load(src_path)

    print(f"重新保存为 {dst_path} ...")
    joblib.dump(model, dst_path)
    print("✅ 完成")

if __name__ == "__main__":
    for src, dst in SRC_MODELS.items():
        try:
            resave_model(src, dst)
        except Exception as e:
            print(f"⚠️ 处理 {src} 出错: {e}")
